<?php

namespace app\common\model;

class MemberCoupons extends Common
{
	protected $insert = ['addtime' => SYS_TIME];
	protected $append = ['url', 'brandname', 'desc', 'tip', 'thumb', 'use_range'];

	protected function getAlldataAttr($value, $data)
	{
		$arr = [0 => '未使用', 1 => '已使用', 2 => '已失效'];
		$adminuser = '--';
		if ($data['adminid']) {
			$ainfo = model('AdminUser')->field('username,realname')->where('userid', $data['adminid'])->find();
			$adminuser = $ainfo['realname'] ?: $ainfo['username'];
		}
		$shareID = (isset($data['ownerid']) && $data['ownerid']) ? $data['ownerid'] : '';
		$data['sharename'] = '';
		if ($shareID) {
			$data['sharename'] = model('AdminUser')->where('userid', $shareID)->value("realname");
		}
		$uinfo = model('AdminUser')->field('username,realname,dealerid')->where('userid', $data['userid'])->find();
		$data['realname'] = $uinfo['realname'] ?: $uinfo['username'];
		$data['username'] = $uinfo['username'] ?: $uinfo['realname'];
		$data['dealername'] = model('AdminUser')->where('userid', $uinfo['dealerid'])->value("username");
		if ($data['type'] == 'activity') {
			$data['typename'] = '活动券';
		} else {
			$data['typename'] = config('COUPONS_TYPE.' . $data['type']);
		}
		$data['statusname'] = $arr[$data['status']];
		$data['adminuser'] = $adminuser;
		$data['usedate'] = $data['usetime'] ? date('Y-m-d H:i:s', $data['usetime']) : '未使用';
		$data['startdate'] = $data['starttime'] ? date('Y-m-d H:i:s', $data['starttime']) : '未设置';
		$data['enddate'] = $data['endtime'] ? date('Y-m-d H:i:s', $data['endtime']) : '未设置';
		$data['adddate'] = $data['addtime'] ? date('Y-m-d H:i:s', $data['addtime']) : '未设置';
		$data['carbrand'] = $this->getBrandnameAttr('', $data);
		$data['activatetime'] = $data['activatetime'];
		$data['tip'] = $data['tip'];
		return $data;
	}

	public function AdminUser()
	{
		return $this->belongsTo('AdminUser', 'userid', 'userid', [], 'LEFT')->setEagerlyType(0);
	}

	protected function getBrandnameAttr($value, $data)
	{
		$name = '';
		if (isset($data['carbrand'])) {
			$carbrandids = explode(',', $data['carbrand']);
			if ($carbrandids) {
				$name1 = model('Cartype')->where('id', $carbrandids[0])->value('title');
				$name2 = '';
				$name3 = '';
				if (count($carbrandids) >= 2) {
					$name2 = model('Cartype')->where('id', $carbrandids[1])->value('title');
					$name = $name1 . ' - ' . $name2;
					if (count($carbrandids) >= 3) {
						$name3 = model('Cartype')->where('id', $carbrandids[2])->value('title');
						$name = $name1 . ' - ' . $name2 . ' - ' . $name3;
					}
				} else {
					$name = $name1;
				}
			}
		}
		return $name;
	}

	protected function getUrlAttr($value, $data)
	{
		$couponsid = $data['couponsid'];
		$couponsrurl = APP_URL . '/index/index/yhq_detail';
		return $couponsrurl;
	}

	protected function getThumbAttr($value, $data)
	{
		$couponsid = $data['couponsid'];
		$image = model('Coupons')->where('id', $couponsid)->value('image');
		return $image;
	}
	protected function getUseRangeAttr($value, $data)
	{
		$dealerid = '';
		if (isset($data['ownerid']) && $data['ownerid']) {
			$dealerid = model('AdminUser')->where('userid', $data['ownerid'])->value('dealerid');
		} else {
			$dealerid = $data['dealerid'];
		}
		$dealername = model('AdminUser')->where('userid', $dealerid)->value('username');
		return $dealername;
	}


	protected function getDescAttr($value, $data)
	{
		if (isset($data['desc']) && $data['desc']) {
			return $data['desc'];
		}
		$couponsid = isset($data['couponsid']) ? $data['couponsid'] : '';
		if ($couponsid != '') {
			$desc = model('Coupons')->where('id', $couponsid)->value('desc');
			return $desc;
		} else {
			return '';
		}
	}

	protected function getTipAttr($value, $data)
	{
		if (isset($data['tip']) && $data['tip']) {
			return $data['tip'];
		}
		$couponsid = isset($data['couponsid']) ? $data['couponsid'] : '';
		if ($couponsid != '') {
			$tip = model('Coupons')->where('id', $couponsid)->value('tip');
			return $tip;
		} else {
			return '';
		}
	}

	// 激活
	public function activate($data)
	{
		$carowner = $data['carowner'];
		if (!$carowner) {
			$this->setError('车主姓名不能为空');
			return false;
		}
		$carbrand = $data['carbrand'];
		if (!$carbrand) {
			$this->setError('车辆品牌不能为空');
			return false;
		}
		$chassisno = $data['chassisno'];
		if (!$chassisno) {
			$this->setError('车架号不能为空');
			return false;
		}
		$licenseno = $data['licenseno'];
		if (!$licenseno) {
			$this->setError('车牌号不能为空');
			return false;
		}
		$couponsinfo = model('MemberCoupons')->where('id', $data['id'])->find();
		if ($couponsinfo) {
			// $couponsinfo['carowner'] = $carowner;
			// $couponsinfo['carbrand'] = $carbrand;
			// $couponsinfo['chassisno'] = $chassisno;
			// $couponsinfo['licenseno'] = $licenseno;
			$data['isactivate'] = '1'; // 已激活
			$this->update_data($data);
			return true;
		} else {
			$this->setError('此免费保养券信息错误，请联系客服人员并核实后重试');
			return false;
		}
	}
}
